package com.prs.crm.service.purchase.impl;

import java.io.File;
import java.io.Serializable;

import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.purchase.PurchaseOrderDao;
import com.prs.crm.domain.purchase.PurchaseOrder;
import com.prs.crm.domain.purchase.PurchaseOrderDetail;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.service.impl.FlowServiceImpl;
import com.prs.crm.service.purchase.PurchaseOrderService;

public class PurchaseOrderServiceImpl extends
		FlowServiceImpl<PurchaseOrder, PurchaseOrderDao> implements
		PurchaseOrderService {
	private PurchaseOrder purchaseOrder;
	private PurchaseOrderService purchaseOrderService;

	@Override
	public boolean valideSubmit(PurchaseOrder entity) {

		return true;
	}

	@Override
	public void savePurchaseOrderDetail(PurchaseOrder purchaseOrder,
			Product[] products, String[] productNames, Double[] markprices,
			Integer[] stockNums, Double[] prices, Double[] discounts,
			Double[] totolMonlys, File file, String fileName) {
		if (file != null) {
			Attach pAttach = FileUtil.getInstance().saveFile(file, fileName,
					"", Constrains.UPLOAD_DIR_PURCHASEORDER);
			purchaseOrder.setPurchaseOrderAttach(pAttach.getSrc());
		}
		for (int i = 0; i < products.length; i++) {
			PurchaseOrderDetail purordetailObject = new PurchaseOrderDetail();
			purordetailObject.setProduct(products[i]);
			purordetailObject.setProducName(productNames[i]);
			purordetailObject.setStockNum(stockNums[i]);
			purordetailObject.setMarkprice(markprices[i]);
			purordetailObject.setPrice(prices[i]);
			purordetailObject.setDiscount(discounts[i]);
			purordetailObject.setTotolMonly(totolMonlys[i]);
			purordetailObject.setInNum(0);
			purordetailObject.setInType(0);
			purchaseOrder.addDetail(purordetailObject);

		}
		purchaseOrder.setPayedMoney(0.0);
		purchaseOrder.setReturnMoney(0.0);
		super.save(purchaseOrder);
	}

	public PurchaseOrder getPurchaseOrder() {
		return purchaseOrder;
	}

	public void setPurchaseOrder(PurchaseOrder purchaseOrder) {
		this.purchaseOrder = purchaseOrder;
	}

	public PurchaseOrderService getPurchaseOrderService() {
		return purchaseOrderService;
	}

	public void setPurchaseOrderService(
			PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	@Override
	public PurchaseOrder getPurchaseOrder(Serializable id) {
		// TODO Auto-generated method stub
		return this.getDao().getPurchaseOrder(id);
	}

	@Override
	public void saveOrUpdate(PurchaseOrder purchaseOrder) {
		this.getDao().saveOrUpdate(purchaseOrder);

	}

}
